package com.youlin.aop;

import com.youlin.config.MyJobListener;
import com.youlin.config.MySchedulerListener;
import com.youlin.config.MyTriggerListener;
import org.redisson.RedissonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.quartz.*;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobKey.jobKey;
import static org.quartz.TriggerKey.triggerKey;
import static org.quartz.impl.matchers.KeyMatcher.*;
import static org.quartz.impl.matchers.KeyMatcher.keyEquals;

@Component
public class MyApplicationListener implements ApplicationListener<ApplicationContextEvent> {

//    @Autowired
    private RedissonNode redissonNode;

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    @Autowired
    private MyTriggerListener myTriggerListener;
    @Autowired
    private MyJobListener myJobListener;
    @Autowired
    private MySchedulerListener mySchedulerListener;

    @Override
    public void onApplicationEvent(ApplicationContextEvent event) {

        try {
//            schedulerFactoryBean.getScheduler().getListenerManager()
//                    .addJobListener(myJobListener, keyEquals(jobKey("helloQuartzJob-1", "helloJobGroup-1")));
//            schedulerFactoryBean.getScheduler().getListenerManager()
//                    .addTriggerListener(myTriggerListener, keyEquals(triggerKey("helloQuartzTrigger-1", "helloTriggerGroup")));
            schedulerFactoryBean.getScheduler().getListenerManager()
                    .addSchedulerListener(mySchedulerListener);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }


//        if (event instanceof ContextRefreshedEvent) {
//            redissonNode.start();
//        }
//        if (event instanceof ContextClosedEvent) {
//            redissonNode.shutdown();
//        }

        /*Scheduler scheduler = schedulerFactoryBean.getScheduler();

        //创建一个Trigger
        TriggerKey triggerKey1 = new TriggerKey("helloQuartzTrigger-1", "helloTriggerGroup");
        Trigger trigger1 = TriggerBuilder.newTrigger()
                .withIdentity(triggerKey1)
                .withSchedule(cronSchedule("0/3 23-29 * * * ?"))
                .build();

        //创建一个job
        JobKey jobKey1 = new JobKey("helloQuartzJob-1", "helloJobGroup");
        JobDetail jobDetail1 = JobBuilder
                .newJob(HelloQuartzJob.class)
                .withIdentity(jobKey1)
                .usingJobData("jk0", "jobDetail1")
                .build();

        TriggerKey triggerKey2 = new TriggerKey("helloQuartzTrigger-2", "helloTriggerGroup");
        Trigger trigger2 = TriggerBuilder.newTrigger()
                .withIdentity(triggerKey2)
                .withSchedule(cronSchedule("0/3 23-29 * * * ?"))
                .build();

        JobDetail jobDetail2 = JobBuilder
                .newJob(HelloQuartzJob.class)
                .withIdentity("helloQuartzJob-2", "helloJobGroup")
                .usingJobData("jk0", "jobDetail2")
                .build();

        Map<JobDetail, Set<? extends Trigger>> triggersAndJobs = new HashMap(){{
            put(jobDetail1, new HashSet<Trigger>(){{add(trigger1);}});
            put(jobDetail2, new HashSet<Trigger>(){{add(trigger2);}});
        }};
        try {
            scheduler.scheduleJobs(triggersAndJobs, false);
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }*/
    }

}
